javascript允许Rhino使用当前项目中的Java类
我很难理解如何将类定义传递给Rhino(使用1.7.13版),以便在JavaScript代码中实例化Java类的对象。 我想做这样的事情
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
public class Main {
public static void main(String args[]){
String script = "var helper = new HelloWorld('Hi World');\n" +
"helper.printMessage()";
Context context = Context.enter();
Scriptable scope = context.initStandardObjects();
context.evaluateString(scope, script,"",1,null);
}
}
我有一个像这样的HelloWorld类
public class HelloWorld {
private String message = "Hello World";
public HelloWorld(){}
public HelloWorld(String message){
this.message = message;
}
public void printMessage(){
System.out.println(message);
}
}
我希望能够根据需要在JavaScript中实例化尽可能多的新对象,这样就不需要在Java端实例化它们。对于引用一个对象,我发现这很有帮助,但它并没有达到我所需要的
Object wrapper = Context.javaToJS(new HelloWorld(), scope);
ScriptableObject.putProperty(scope, "myHelloWorld", wrapper);
我该怎么做那样的事
共 (0) 个答案